Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
typeof-properties
Advanced tools
typeof-properties
validates the object's properties' type.
of-type
package to check if the given value|object is of expected type.typeof-arguments
to validate the arguments' types passed through the enclosing function.npm install typeof-properties
const type = require('typeof-properties');
typeof-properties.js
library to the HTML file.The library is located in ./dist/typeof-properties.js
directory.
It is a webpack&babel bundled cross-browser library version.
The library is accessible as typeofProperties
variable in the global (window) scope.
<head>
<script src='typeof-properties.js'></script>
<script>
var person = { name: 'Nikola', age: 26 }
typeofProperties(person, { name: String, age: 'number' });
</script>
</head>
> git clone https://github.com/devrafalko/typeof-properties.git
> cd typeof-properties
> npm install
> npm test //run tests in node
> npm test deep //run tests in node with errors shown
type(actual, expected[, callback])
actual
[Object]actual
object will be validated.expected
[Object]expected
object should contain properties, that cohere with the properties of actual
objectexpected
object's properties should indicate the expected type of the coherent properties of actual
objectactual
properties are ommited in expected
object, they will not be validated (can be of any type)expected
TypesThere are four ways to check the type of properties:
null
or undefined
valuesMind, that the
typeof-properties
library uses theof-type
library as the dependency, to validate the types. If you feel confused how to use the types, see more samples here.
[String]
'null'
, 'undefined'
constructor.name
, eg:'string'
, 'number'
, 'regexp'
, 'array'
, 'object'
, 'boolean'
,'buffer'
, etc.'String'
, 'string'
, 'StRiNg'
checks if the property is of [String]
type'RegExp'
, 'REGEXP'
, 'regexp'
checks if the property is of [RegExp]
type|
:
'array|object'
checks if the property is of [Array]
OR
[Object]
type'undefined|null'
checks if the property is of undefined
OR
null
typeconst actual = {
name: 'Nikola',
age: 26
};
const expected = {
name: 'string',
age: 'number|string|undefined'
};
type(actual, expected);
[RegExp]
/null/
, /undefined/
constructor.name
, eg: /String/
, /Number/
, /RegExp/
, /Array/
, /Object/
, /Boolean/
,/Buffer/
, /Promise/
, etc./Str/
, /Err/
, /Reg/
, /B/
/.+Error$/
, /^RegExp$/
,/^[A-Z][a-z]+$/
i
flag:
/string/i
, /regexp/i
, /TYPEERROR/i
(x|y)
expression:
/String|Number/
, /TypeError|Error/
, /(obj|str)/i
const actual = {
name: 'Nikola',
age: 26
};
const expected = {
name: /string/i,
age: /Number|String|undefined/
};
type(actual, expected);
[Function|Array|null|undefined]
null
, undefined
[Function]
constructor, eg: String
, TypeError
, Promise
, Array
, etc.[String, Object, Array, null]
[null, undefined, Boolean]
const actual = {
name: 'Nikola',
age: 26
};
const expected = {
name: String,
age: [Number, String, undefined]
};
type(actual, expected);
When you use bundlers or minifiers, use
[String|RegExp]
type wisely as bundlers may change the names of functions|constructors|classes in the output file and eg.
type({name: new Name('Nikola')}, {name: 'Name'});
that is valid before compilation, may fail after compilation, if the bundler minifies the'Name'
constructor name.
[String] 'arguments'
| [RegExp] /arguments/
'arguments'
or /arguments/
expects the property's value to be the function's arguments
object[String] 'instance'
| [RegExp] /instance/
'instance'
or /instance/
expects the property's value to be the instance of the user's class|constructor[]
, 'hello world'
, {}
global
|window
's properties[String] 'objectable'
| [RegExp] /objectable/
'objectable'
or /objectable/
expects the property's value to be the object that is the instance of the Object
constructor
{}
, []
, new String('hello world')
, new Boolean(1)
'hello world'
, true
, 10
, null
, undefined
[String] 'truthy'
| [RegExp] /truthy/
'truthy'
or /truthy/
expects the property's value to be like:
'abc'
, true
, 1
, -1
, {}
, []
, function(){}
[String] 'falsy'
| [RegExp] /falsy/
'falsy'
or /falsy/
expects the property's value to be like:
''
, false
, 0
, null
, undefined
, NaN
[String] 'any'
| [RegExp] /any/
| [Array] []
| [String] ""
type
'any'
or /any/
or empty array []
or empty string ""
expects the property's value to be of any typecallback
[Function] (optional)Invalid property ["name"]. The [undefined] value has been assigned, while the value of type matching string expression "string|null" is expected.
Invalid property ["name"]. The [undefined] <<falsy>> value has been assigned, while the value of type matching string expression "truthy|null" is expected.
Invalid property ["name"]. The [undefined] value has been assigned, while the value of type matching regular expression /String|null/ is expected.
Invalid property ["name"]. The [undefined] value has been assigned, while the value of type [String|null] is expected.
callback
function.throw
statement!callback
function is executed only if at least one property's value is of invalid type.callback
function with the following properties:
name
"name"
, "age"
actual
"String"
expected
"Array"
, "Boolean|Number"
, "/array|object/i"
message
textActual
"[undefined] <<falsy>> value"
textExpected
"value of type matching regular expression /String|null/"
const type = require('typeof-properties');
const person = {
name: 'Nikola',
age: '27',
experience: 7,
male: true,
skills: ['js', 'nodejs', 'mongodb'],
talk: function () {
return `hello I'm ${this.name}`;
}
};
const validation = {
name: 'string',
age: /(number|string)/i,
talk: 'function|falsy',
skills: [Array, Object, null],
male: Boolean
//experience property is ommited - not validated - can be of any type
};
type(person, validation, (o) => {
console.error(o.message);
/*
console.error(`Not good! Use ${o.expected} instead of ${o.actual} for the property ${o.name}`);
throw new TypeError('Aborted: ' + o.message);
*/
});
The function type()
returns true
when all checked properties are of valid types.
The function type()
returns false
when at least one of the checked properties is of invalid type.
if (!type(person, validation, () => console.log('Aborted.'))) return;
const type = require('typeof-properties');
const weatherData = {
city: 'Warsaw',
latitude: 52.229676,
longitude: 21.012229,
date: new Date('2017-08-22'),
temperature: { day: 24, night: 18 },
humidity: .71,
winter: { kph: 18, mph: 11.3 }
};
const weatherValid = {
city: 'string',
latitude: [Number, String],
longitude: [Number, String],
date: 'date',
temperature: /object|number/i,
humidity: 'number|falsy',
winter: 'object|number'
};
const tempValid = {
day: 'number|undefined',
night: [Number, undefined]
};
const winterValid = {
kph: 'number|falsy',
mph: /number|falsy/i
};
type(weatherData, weatherValid);
type(weatherData.temperature, tempValid);
type(weatherData.winter, winterValid);
const type = require('typeof-properties');
class Person {
constructor(name, age, earnings) {
this.name = name;
this.age = age;
this.earnings = earnings;
}
}
class Earnings {
constructor(income, tax) {
this.income = income;
this.tax = tax;
}
}
const earnings = new Earnings(23000, 6400);
const person = new Person('Jessica', 22, earnings);
const expected = {
name: String,
age: 'number',
earnings: 'instance'
};
type(person, expected);
FAQs
Validate the type of object's properties.
The npm package typeof-properties receives a total of 533 weekly downloads. As such, typeof-properties popularity was classified as not popular.
We found that typeof-properties demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.